<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>A Bytecode Virtual Machine &middot; Crafting Interpreters</title>

<!-- Tell mobile browsers we're optimized for them and they don't need to crop
     the viewport. -->
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" type="text/css" href="style.css" />

<!-- Oh, God, Source Code Pro is so beautiful it makes me want to cry. -->
<link href='http://fonts.googleapis.com/css?family=Source+Code+Pro:400|Source+Sans+Pro:300,400,600' rel='stylesheet' type='text/css'>
<link rel="icon" type="image/png" href="image/favicon.png" />
<script src="jquery-1.10.1.min.js"></script>
<script src="script.js"></script>

<!-- Google analytics -->
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-42804721-2', 'auto');
  ga('send', 'pageview');
</script>

</head>
<body id="top">

<!-- <div class="scrim"></div> -->
<nav class="wide">
  <a href="/"><img src="image/logotype-small.png" title="Crafting Interpreters"></a>
  <div class="contents">
<!-- A title with no part means it's a part chapter. -->
<h2><small>III</small>A Bytecode Virtual Machine</h2>

<ul>
    <li><a href="chunks-of-bytecode.html"><small>14</small>Chunks of Bytecode</a></li>
    <li><a href="a-virtual-machine.html"><small>15</small>A Virtual Machine</a></li>
    <li><a href="scanning-on-demand.html"><small>16</small>Scanning on Demand</a></li>
    <li><a href="compiling-expressions.html"><small>17</small>Compiling Expressions</a></li>
    <li><a href="types-of-values.html"><small>18</small>Types of Values</a></li>
    <li><a href="strings.html"><small>19</small>Strings</a></li>
    <li><a href="hash-tables.html"><small>20</small>Hash Tables</a></li>
    <li><a href="global-variables.html"><small>21</small>Global Variables</a></li>
    <li><a href="local-variables.html"><small>22</small>Local Variables</a></li>
    <li><a href="jumping-forward-and-back.html"><small>23</small>Jumping Forward and Back</a></li>
    <li><a href="calls-and-functions.html"><small>24</small>Calls and Functions</a></li>
    <li><a href="closures.html"><small>25</small>Closures</a></li>
    <li><a href="garbage-collection.html"><small>26</small>Garbage Collection</a></li>
    <li><a href="classes-and-instances.html"><small>27</small>Classes and Instances</a></li>
    <li><a href="methods-and-initializers.html"><small>28</small>Methods and Initializers</a></li>
    <li><a href="superclasses.html"><small>29</small>Superclasses</a></li>
    <li><a href="optimization.html"><small>30</small>Optimization</a></li>
</ul>


<div class="prev-next">
    <a href="inheritance.html" title="Inheritance" class="left">&larr;&nbsp;Previous</a>
    <a href="contents.html" title="Table of Contents">&uarr;&nbsp;Up</a>
    <a href="chunks-of-bytecode.html" title="Chunks of Bytecode" class="right">Next&nbsp;&rarr;</a>
</div>  </div>
</nav>

<nav class="narrow">
<a href="/"><img src="image/logotype-small.png" title="Crafting Interpreters"></a>
<a href="inheritance.html" title="Inheritance" class="prev">←</a>
<a href="chunks-of-bytecode.html" title="Chunks of Bytecode" class="next">→</a>
</nav>

<div class="page">
<div class="nav-wrapper">
<nav class="floating">
  <a href="/"><img src="image/logotype-small.png" title="Crafting Interpreters"></a>
  <div class="expandable">
<!-- A title with no part means it's a part chapter. -->
<h2><small>III</small>A Bytecode Virtual Machine</h2>

<ul>
    <li><a href="chunks-of-bytecode.html"><small>14</small>Chunks of Bytecode</a></li>
    <li><a href="a-virtual-machine.html"><small>15</small>A Virtual Machine</a></li>
    <li><a href="scanning-on-demand.html"><small>16</small>Scanning on Demand</a></li>
    <li><a href="compiling-expressions.html"><small>17</small>Compiling Expressions</a></li>
    <li><a href="types-of-values.html"><small>18</small>Types of Values</a></li>
    <li><a href="strings.html"><small>19</small>Strings</a></li>
    <li><a href="hash-tables.html"><small>20</small>Hash Tables</a></li>
    <li><a href="global-variables.html"><small>21</small>Global Variables</a></li>
    <li><a href="local-variables.html"><small>22</small>Local Variables</a></li>
    <li><a href="jumping-forward-and-back.html"><small>23</small>Jumping Forward and Back</a></li>
    <li><a href="calls-and-functions.html"><small>24</small>Calls and Functions</a></li>
    <li><a href="closures.html"><small>25</small>Closures</a></li>
    <li><a href="garbage-collection.html"><small>26</small>Garbage Collection</a></li>
    <li><a href="classes-and-instances.html"><small>27</small>Classes and Instances</a></li>
    <li><a href="methods-and-initializers.html"><small>28</small>Methods and Initializers</a></li>
    <li><a href="superclasses.html"><small>29</small>Superclasses</a></li>
    <li><a href="optimization.html"><small>30</small>Optimization</a></li>
</ul>


<div class="prev-next">
    <a href="inheritance.html" title="Inheritance" class="left">&larr;&nbsp;Previous</a>
    <a href="contents.html" title="Table of Contents">&uarr;&nbsp;Up</a>
    <a href="chunks-of-bytecode.html" title="Chunks of Bytecode" class="right">Next&nbsp;&rarr;</a>
</div>  </div>
  <a id="expand-nav">≡</a>
</nav>
</div>

<article class="chapter">

  <div class="number">III</div>
  <h1 class="part">A Bytecode Virtual Machine</h1>

<div class="sign-up closable">
    <h1>This book is a work in progress!</h1>
    <span class="dismiss">&times;</span>
    <p>If you see a mistake, find something unclear, or have a suggestion, please <a href="https://github.com/munificent/craftinginterpreters/issues" target="_blank">let me know</a>. To learn when new chapters are up, join the mailing list:</p>

  <!-- Begin MailChimp Signup Form -->
  <div id="mc_embed_signup">
  <form action="//gameprogrammingpatterns.us7.list-manage.com/subscribe/post?u=0952ca43ed2536d6717766b88&amp;id=6e96334109" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
    <input type="email" value="" name="EMAIL" class="email" id="mce-EMAIL" placeholder="Your email address" required>
    <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
    <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_0952ca43ed2536d6717766b88_6e96334109" tabindex="-1" value=""></div>
    <input type="submit" value="Sign me up!" name="subscribe" id="mc-embedded-subscribe" class="button">
  </form>
  </div>
  <!--End mc_embed_signup-->
  <p class="small">(I post about once a month. Don&#8217;t worry, I won&#8217;t spam you.)</p>
</div>

<p>Our Java interpreter, jlox, covers a lot of territory, but it&rsquo;s not perfect.
First, if you run any interesting Lox programs on it, you&rsquo;ll discover it&rsquo;s
achingly slow. The style of interpretation it uses<span class="em">&mdash;</span>walking the AST directly<span class="em">&mdash;</span>is good enough for <em>some</em> real-world uses, but leaves a lot to be desired for
a general-purpose scripting language.</p>
<p>Also, we implicitly rely on some fundamental stuff from the JVM itself. We take
for granted that things like <code>instanceof</code> in Java work <em>somehow</em>. And we never
for a second worry about memory management because the JVM&rsquo;s garbage collector
takes care of it for us.</p>
<p>Those were useful training wheels while we were learning the basic concepts
around interpreters. But now that we have those concepts down, it&rsquo;s time for us
to take those wheels off and build our own virtual machine from scratch using
nothing more than the C standard library&hellip;</p>

<footer>
<a href="chunks-of-bytecode.html" class="next">
  Next Chapter: &ldquo;Chunks of Bytecode&rdquo; &rarr;
</a>
Hand-crafted by Robert Nystrom&ensp;&mdash;&ensp;<a href="https://github.com/munificent/craftinginterpreters/blob/master/LICENSE" target="_blank">&copy; 2015&hairsp;&ndash;&hairsp;2018</a>
</footer>
</article>

</div>
</body>
</html>